﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Notice")]
    public class T_Notice : IEntity<T_Notice>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Title { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string SubTitle { get; set; }

        public DateTime CreateTime { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Promulgator { get; set; }

        [Column(TypeName = "ntext")]
        public string Content { get; set; }

        public override T_Notice Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Notice Find(System.Linq.Expressions.Expression<Func<T_Notice, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Notice.FirstOrDefault(where);
                if (item != null)
                {
                    this.Content = item.Content;
                    this.CreateTime = item.CreateTime;
                    this.Id = item.Id;
                    this.Promulgator = item.Promulgator;
                    this.SubTitle = item.SubTitle;
                    this.Title = item.Title;
                    return this;
                }
                return null;
            }
        }

        public override T_Notice FindLast(System.Linq.Expressions.Expression<Func<T_Notice, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Notice.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Content = item.Content;
                    this.CreateTime = item.CreateTime;
                    this.Id = item.Id;
                    this.Promulgator = item.Promulgator;
                    this.SubTitle = item.SubTitle;
                    this.Title = item.Title;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Notice, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Notice.Any(where);
            }
        }
    }
}
